<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0px;margin: 0px;}
        .box1{
            width: 20px;
            height: 400px;
            background-color: aqua;
            position: relative;
            margin: auto;
        }
        .circle1{
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: red;
            position: absolute;
            left: -10px;
            top: 180px;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="circle1"></div>
    </div>
</body>
<script>
    var circle1=document.getElementsByClassName("circle1")[0]
    var box1=document.getElementsByClassName("box1")[0]
    circle1.onmousedown=mousedown;
    function mousedown(e1){
        var offsetTop=circle1.offsetTop;//初始值
        var startY=e1.offsetY-circle1.clientHeight/2;
       if(offsetTop+startY<0){
        circle1.style.top=0+"px";
       }else if(offsetTop+startY>360){
         circle1.style.top=360+"px";
       }
       else{
         circle1.style.top=offsetTop+startY+"px";
       }
       
        circle1.onmousemove=mousemove;
        box1.onmouseup=mouseup;
        function mousemove(e2){
            var offsetTop=circle1.offsetTop;//新当前值
             var moveY=e2.offsetY-circle1.clientHeight/2;
             if(offsetTop+moveY<0){
        circle1.style.top=0+"px";
       }else if(offsetTop+moveY>360){
         circle1.style.top=360+"px";
       }
       else{
         circle1.style.top=offsetTop+moveY+"px";
       }
         }
       function mouseup(){
        console.log("111");
        circle1.onmousemove=null;
       } 
    }
</script>
</html>